﻿using System.Net;
using System.Xml.Linq;

namespace Boco.Digg.Service
{
	public class DiggGateway : IDiggGateway
	{
		static WebClient _webClient;

		// Get these from a config file
		static string _apiKey = @"http://www.testboco.com";
		static string _serviceUrl = @"http://services.digg.com";
		
		public DiggGateway()
		{
			_webClient = new WebClient();
			_webClient.Headers[HttpRequestHeader.UserAgent] = ".NET/2.0";
		}

		public XDocument ExecuteMethod(IMethod method)
		{
			string fullRequest = BuildRequestString(method);
			string rawXml = _webClient.DownloadString(fullRequest);

			return XDocument.Parse(rawXml);
		}

		public string BuildRequestString(IMethod method)
		{
			return string.Format("{0}/{1}&appkey={2}",
				_serviceUrl, method, _apiKey);
		}
	}
}